package cc.rengu.igas.channel.wechat.common.dao.impl;

import cc.rengu.igas.channel.wechat.common.dao.TemplateCfgMapper;
import cc.rengu.igas.channel.wechat.common.entity.TemplateCfg;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

public class TemplateCfgMapperImpl implements TemplateCfgMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    @Override
    public TemplateCfg selectTemplateCfgByInstIdAndType(String instId, String templateType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTemplateCfgByInstIdAndType");
        TemplateCfg templateCfg = new TemplateCfg();
        templateCfg.setInstId(instId);
        templateCfg.setTemplateType(templateType);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, templateCfg, templateCfg);
        if (0 != iReturnCode) {
            rglog.error("根据机构号instId:<{}>、模板类型templateType:<{}>获取消息模板信息失败,,iReturnCode:<{}>", instId, templateType, iReturnCode);
            return null;
        }
        return templateCfg;
    }
}
